package com.ymttest.business.service.shenqi;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;

import com.shop2cn.iapi.cdfgeteway.parameter.GetUserPointsReq;
import com.shop2cn.iapi.cdfgeteway.resp.PointsVo;
import com.shop2cn.iapi.cdfgeteway.service.GetUserPointsCall;
import com.shop2cn.iapi.points.enums.PointsApplyUserTypeEnum;
import com.shop2cn.iapi.points.enums.PointsBizTypeEnum;
import com.shop2cn.iapi.points.enums.PointsSendTypeEnum;
import com.shop2cn.iapi.points.parameter.CalcOrderCreditReq;
import com.shop2cn.iapi.points.parameter.SaveCreditPolicyReq;
import com.shop2cn.iapi.points.parameter.SendCreditReq;
import com.shop2cn.iapi.points.pojo.CreditPolicy;
import com.shop2cn.iapi.points.resp.CalcOrderCreditResp;
import com.shop2cn.iapi.points.service.CalcOrderCreditCall;
import com.shop2cn.iapi.points.service.SaveCreditPolicyCall;
import com.shop2cn.iapi.points.service.SendCreditCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.TestDataManager;

public class GodPointsService {
	
	/**
	 * 设置商家积分策略
	 * 
	 * @param shopId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void saveCreditPolicyReqWithSHARE_SEND(Long shopId, CreditPolicy creditPolicy_SHARE_SEND)
			throws ClientProtocolException, IOException {

		SaveCreditPolicyReq saveCreditPolicyReq = new SaveCreditPolicyReq();
		SaveCreditPolicyCall saveCreditPolicyCall = new SaveCreditPolicyCall();

		saveCreditPolicyReq.setShopId(shopId);
		saveCreditPolicyReq.setTitle("自动化测试积分策略");
		saveCreditPolicyReq.setUseMinLimit(10);
		saveCreditPolicyReq.setApplyUserType(PointsApplyUserTypeEnum.MERCHANT
				.getCode());
		saveCreditPolicyReq.setOrderDeductLimit(90);
		saveCreditPolicyReq.setSendType(PointsSendTypeEnum.PAID.getCode());
		saveCreditPolicyReq.setSharePerDayLimit(0);
		saveCreditPolicyReq.setShippedDays(0);

		CreditPolicy creditPolicy_ORDER_SEND = new CreditPolicy();

		creditPolicy_ORDER_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_ORDER_SEND.setBizType(PointsBizTypeEnum.ORDER_SEND
				.getCode());
		creditPolicy_ORDER_SEND.setCredit(1);
		creditPolicy_ORDER_SEND.setCurrency("CNY");
		
		CreditPolicy creditPolicy_ORDER_DEDUCT = new CreditPolicy();

		creditPolicy_ORDER_DEDUCT.setAmount(BigDecimal.TEN);
		creditPolicy_ORDER_DEDUCT.setBizType(PointsBizTypeEnum.ORDER_DEDUCT
				.getCode());
		creditPolicy_ORDER_DEDUCT.setCredit(1);
		creditPolicy_ORDER_DEDUCT.setCurrency("CNY");

		CreditPolicy creditPolicy_RECHARGE_SEND = new CreditPolicy();

		creditPolicy_RECHARGE_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_RECHARGE_SEND.setBizType(PointsBizTypeEnum.RECHARGE_SEND
				.getCode());
		creditPolicy_RECHARGE_SEND.setCredit(1);
		creditPolicy_RECHARGE_SEND.setCurrency("CNY");

		ArrayList<CreditPolicy> policyList = new ArrayList<>();

		policyList.add(creditPolicy_ORDER_SEND);
		policyList.add(creditPolicy_ORDER_DEDUCT);
		policyList.add(creditPolicy_RECHARGE_SEND);
		policyList.add(creditPolicy_SHARE_SEND);

		saveCreditPolicyReq.setPolicyList(policyList);

		saveCreditPolicyCall.setData(saveCreditPolicyReq);
		saveCreditPolicyCall.callService();
	}
	
	/**
	 * 设置商家积分策略
	 * 
	 * @param shopId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void saveCreditPolicyReqWithORDER_DEDUCT(Long shopId, CreditPolicy creditPolicy_ORDER_DEDUCT)
			throws ClientProtocolException, IOException {

		SaveCreditPolicyReq saveCreditPolicyReq = new SaveCreditPolicyReq();
		SaveCreditPolicyCall saveCreditPolicyCall = new SaveCreditPolicyCall();

		saveCreditPolicyReq.setShopId(shopId);
		saveCreditPolicyReq.setTitle("自动化测试积分策略");
		saveCreditPolicyReq.setUseMinLimit(10);
		saveCreditPolicyReq.setApplyUserType(PointsApplyUserTypeEnum.MERCHANT
				.getCode());
		saveCreditPolicyReq.setOrderDeductLimit(90);
		saveCreditPolicyReq.setSendType(PointsSendTypeEnum.PAID.getCode());
		saveCreditPolicyReq.setSharePerDayLimit(0);
		saveCreditPolicyReq.setShippedDays(0);

		CreditPolicy creditPolicy_ORDER_SEND = new CreditPolicy();

		creditPolicy_ORDER_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_ORDER_SEND.setBizType(PointsBizTypeEnum.ORDER_SEND
				.getCode());
		creditPolicy_ORDER_SEND.setCredit(1);
		creditPolicy_ORDER_SEND.setCurrency("CNY");

		CreditPolicy creditPolicy_RECHARGE_SEND = new CreditPolicy();

		creditPolicy_RECHARGE_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_RECHARGE_SEND.setBizType(PointsBizTypeEnum.RECHARGE_SEND
				.getCode());
		creditPolicy_RECHARGE_SEND.setCredit(1);
		creditPolicy_RECHARGE_SEND.setCurrency("CNY");

		CreditPolicy creditPolicy_SHARE_SEND = new CreditPolicy();

		creditPolicy_SHARE_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_SHARE_SEND.setBizType(PointsBizTypeEnum.SHARE_SEND
				.getCode());
		creditPolicy_SHARE_SEND.setCredit(100000);
		creditPolicy_SHARE_SEND.setCurrency("");

		ArrayList<CreditPolicy> policyList = new ArrayList<>();

		policyList.add(creditPolicy_ORDER_SEND);
		policyList.add(creditPolicy_ORDER_DEDUCT);
		policyList.add(creditPolicy_RECHARGE_SEND);
		policyList.add(creditPolicy_SHARE_SEND);

		saveCreditPolicyReq.setPolicyList(policyList);

		saveCreditPolicyCall.setData(saveCreditPolicyReq);
		saveCreditPolicyCall.callService();
	}
	
	/**
	 * 设置商家积分策略
	 * 
	 * @param shopId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void saveCreditPolicyReq(Long shopId)
			throws ClientProtocolException, IOException {

		SaveCreditPolicyReq saveCreditPolicyReq = new SaveCreditPolicyReq();
		SaveCreditPolicyCall saveCreditPolicyCall = new SaveCreditPolicyCall();

		saveCreditPolicyReq.setShopId(shopId);
		saveCreditPolicyReq.setTitle("自动化测试积分策略");
		saveCreditPolicyReq.setUseMinLimit(10);
		saveCreditPolicyReq.setApplyUserType(PointsApplyUserTypeEnum.MERCHANT
				.getCode());
		saveCreditPolicyReq.setOrderDeductLimit(90);
		saveCreditPolicyReq.setSendType(PointsSendTypeEnum.PAID.getCode());
//		saveCreditPolicyReq.setSendType(PointsSendTypeEnum.SHIPPED_DAYS.getCode());
		saveCreditPolicyReq.setSharePerDayLimit(0);
		saveCreditPolicyReq.setShippedDays(0);

		CreditPolicy creditPolicy_ORDER_SEND = new CreditPolicy();

		creditPolicy_ORDER_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_ORDER_SEND.setBizType(PointsBizTypeEnum.ORDER_SEND
				.getCode());
		creditPolicy_ORDER_SEND.setCredit(1);
		creditPolicy_ORDER_SEND.setCurrency("CNY");

		CreditPolicy creditPolicy_ORDER_DEDUCT = new CreditPolicy();

		creditPolicy_ORDER_DEDUCT.setAmount(BigDecimal.TEN);
		creditPolicy_ORDER_DEDUCT.setBizType(PointsBizTypeEnum.ORDER_DEDUCT
				.getCode());
		creditPolicy_ORDER_DEDUCT.setCredit(1);
		creditPolicy_ORDER_DEDUCT.setCurrency("CNY");

		CreditPolicy creditPolicy_RECHARGE_SEND = new CreditPolicy();

		creditPolicy_RECHARGE_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_RECHARGE_SEND.setBizType(PointsBizTypeEnum.RECHARGE_SEND
				.getCode());
		creditPolicy_RECHARGE_SEND.setCredit(1);
		creditPolicy_RECHARGE_SEND.setCurrency("CNY");

		CreditPolicy creditPolicy_SHARE_SEND = new CreditPolicy();

		creditPolicy_SHARE_SEND.setAmount(BigDecimal.TEN);
		creditPolicy_SHARE_SEND.setBizType(PointsBizTypeEnum.SHARE_SEND
				.getCode());
		creditPolicy_SHARE_SEND.setCredit(100000);
		creditPolicy_SHARE_SEND.setCurrency("");

		// CreditPolicy creditPolicy_ORDER_CANCEL_RETURN = new CreditPolicy();
		//
		// creditPolicy_ORDER_CANCEL_RETURN.setAmount(BigDecimal.TEN);
		// creditPolicy_ORDER_CANCEL_RETURN.setBizType(PointsBizTypeEnum.ORDER_CANCEL_RETURN.getCode());
		// creditPolicy_ORDER_CANCEL_RETURN.setCredit(1);
		// creditPolicy_ORDER_CANCEL_RETURN.setCurrency("CNY");
		//
		// CreditPolicy creditPolicy_ORDER_CANCEL_RECYCLE = new CreditPolicy();
		//
		// creditPolicy_ORDER_CANCEL_RECYCLE.setAmount(BigDecimal.TEN);
		// creditPolicy_ORDER_CANCEL_RECYCLE.setBizType(PointsBizTypeEnum.ORDER_CANCEL_RECYCLE.getCode());
		// creditPolicy_ORDER_CANCEL_RECYCLE.setCredit(1);
		// creditPolicy_ORDER_CANCEL_RECYCLE.setCurrency("CNY");
		//
		// CreditPolicy creditPolicy_REFUND_RETURN = new CreditPolicy();
		//
		// creditPolicy_REFUND_RETURN.setAmount(BigDecimal.TEN);
		// creditPolicy_REFUND_RETURN.setBizType(PointsBizTypeEnum.REFUND_RETURN.getCode());
		// creditPolicy_REFUND_RETURN.setCredit(1);
		// creditPolicy_REFUND_RETURN.setCurrency("CNY");
		//
		// CreditPolicy creditPolicy_REFUND_RECYCLE = new CreditPolicy();
		//
		// creditPolicy_REFUND_RECYCLE.setAmount(BigDecimal.TEN);
		// creditPolicy_REFUND_RECYCLE.setBizType(PointsBizTypeEnum.REFUND_RECYCLE.getCode());
		// creditPolicy_REFUND_RECYCLE.setCredit(1);
		// creditPolicy_REFUND_RECYCLE.setCurrency("CNY");
		//
		// CreditPolicy creditPolicy_ORDER_CREATE_FAIL_RETURN = new
		// CreditPolicy();
		//
		// creditPolicy_ORDER_CREATE_FAIL_RETURN.setAmount(BigDecimal.TEN);
		// creditPolicy_ORDER_CREATE_FAIL_RETURN.setBizType(PointsBizTypeEnum.ORDER_CREATE_FAIL_RETURN.getCode());
		// creditPolicy_ORDER_CREATE_FAIL_RETURN.setCredit(1);
		// creditPolicy_ORDER_CREATE_FAIL_RETURN.setCurrency("CNY");

		ArrayList<CreditPolicy> policyList = new ArrayList<>();

		policyList.add(creditPolicy_ORDER_SEND);
		policyList.add(creditPolicy_ORDER_DEDUCT);
		policyList.add(creditPolicy_RECHARGE_SEND);
		policyList.add(creditPolicy_SHARE_SEND);
		// policyList.add(creditPolicy_ORDER_CANCEL_RETURN);
		// policyList.add(creditPolicy_ORDER_CANCEL_RECYCLE);
		// policyList.add(creditPolicy_REFUND_RETURN);
		// policyList.add(creditPolicy_REFUND_RECYCLE);
		// policyList.add(creditPolicy_ORDER_CREATE_FAIL_RETURN);

		saveCreditPolicyReq.setPolicyList(policyList);

		saveCreditPolicyCall.setData(saveCreditPolicyReq);
		saveCreditPolicyCall.callService();
	}

	/**
	 * 
	 * @param shopId
	 * @param userId
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static void sendCredit(Long shopId, Long userId)
			throws ClientProtocolException, IOException {

		SendCreditReq sendCreditReq = new SendCreditReq();
		SendCreditCall sendCreditCall = new SendCreditCall();

		sendCreditReq.setShopId(shopId);
		sendCreditReq.setUserId(userId);
		sendCreditReq.setBizNo(TestDataManager.randomStr(10));
		sendCreditReq.setBizType(PointsBizTypeEnum.SHARE_SEND.getCode());
		sendCreditReq.setUserType(PointsApplyUserTypeEnum.MERCHANT.getCode());

		sendCreditCall.setData(sendCreditReq);
		sendCreditCall.callService();
	}

	/**
	 * 订单可使用积分
	 * 
	 * @param amount
	 *            积分抵扣前的订单可支付金额
	 * @param currency
	 * @param shopId
	 * @param userId
	 * @throws Exception
	 */
	public static CalcOrderCreditResp calcOrderCredit(BigDecimal amount,
			String currency, Long shopId, Long userId) throws Exception {

		CalcOrderCreditReq calcOrderCreditReq = new CalcOrderCreditReq();
		CalcOrderCreditCall calcOrderCreditCall = new CalcOrderCreditCall();

		calcOrderCreditReq.setAmount(amount);
		calcOrderCreditReq.setCurrency(currency);
		calcOrderCreditReq.setShopId(shopId);
		calcOrderCreditReq.setUserId(userId);
		calcOrderCreditReq.setUserType(PointsApplyUserTypeEnum.MERCHANT
				.getCode());

		calcOrderCreditCall.setData(calcOrderCreditReq);
		calcOrderCreditCall.callService();

		return calcOrderCreditCall.getCalcOrderCreditResp();
	}
	
	/**
	 * 根据积分策略和使用的积分计算积分抵扣金额
	 * @param calcOrderCreditResp
	 * @param usedCredit
	 * @return
	 */
	public static BigDecimal calcOrderCreditAmount(CalcOrderCreditResp calcOrderCreditResp, Long usedCredit) {
		
		BigDecimal multiple = new BigDecimal(usedCredit.toString()).divide(new BigDecimal(
				calcOrderCreditResp.getCreditPolicy().toString()), 2,
				BigDecimal.ROUND_HALF_DOWN);
		
		return multiple.multiply(
				new BigDecimal(calcOrderCreditResp.getAmountPolicy().toString()))
				.setScale(2, BigDecimal.ROUND_HALF_DOWN);
	}
	
	public static PointsVo getCDFMemberUserPoints(Long sellerId, Long userId) throws Exception {
		
		refreshToken();
		
		GetUserPointsReq getUserPointsReq = new GetUserPointsReq();
		GetUserPointsCall getUserPointsCall = new GetUserPointsCall();
		
		getUserPointsReq.setShopId(sellerId);
		getUserPointsReq.setUserId(userId);
		
		getUserPointsCall.setData(getUserPointsReq);
		getUserPointsCall.callService();
		
		return getUserPointsCall.getPointsVo();
	}
	
	/**
	 * 刷新中免大会员token
	 */
	public static void refreshToken() {
		
		YmatouEasyCall refreshTokenCall = new YmatouEasyCall("", "POST", "JSON");

		refreshTokenCall
				.setUrl("http://cdfgeteway.iapi.shop2cn.com/cdf/refreshToken");

		refreshTokenCall.setData("{}");

		try {
			refreshTokenCall.callService();

			Logger.comment(refreshTokenCall.getReturnData());

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {

//		CalcOrderCreditResp calcOrderCreditResp = GodPointsService.calcOrderCredit(
//				new BigDecimal("5299.00"), "CNY", 501989378L, 20238699L);
		
		System.out.println(GodPointsService.getCDFMemberUserPoints(502060981L, 26080783L));
	}
}
